package com.it.water.utils;

import cn.hutool.core.util.StrUtil;
import com.it.water.entity.ApiConstant;
import com.it.water.exception.ServiceException;

/**
 * 断言工具类
 *
 * @author HelloWorld505
 * @version 1.0
 * @since 2024-03-17
 */
public class AssertUtil {

    /**
     * 判断字符串非空
     */
    public static void isNotEmpty(String str, String... message) {
        if (StrUtil.isBlank(str)) {
            execute(message);
        }
    }

    /**
     * 判断对象非空
     */
    public static void isNotNull(Object obj, String... message) {
        if (obj == null) {
            execute(message);
        }
    }

    /**
     * 判断结果是否为真
     */
    public static void isTrue(boolean isTrue, String... message) {
        if (isTrue) {
            execute(message);
        }
    }

    private static void execute(String... message) {
        String msg = ApiConstant.ERROR_MESSAGE;
        if (message != null && message.length > 0) {
            msg = message[0];
        }
        // 抛出 ServiceException
        throw new ServiceException(msg);
    }
}
